﻿using System;

using OpenTK;

namespace MosCraft.Utilities
{
    static class QuickMath
    {
        private static float[] SIN_TABLE;

        static QuickMath()
        {
            SIN_TABLE = new float[0x10000];
            for (int i = 0; i < 0x10000; i++)
            {
                SIN_TABLE[i] = (float)Math.Sin(((double)i * 3.1415926535897931D * 2D) / 65536D);
            }
        }

        public static float Sin(float f)
	    {
		    return SIN_TABLE[(int) (f * 10430.38F) & 0xffff];
	    }

	    public static float Cos(float f)
	    {
		    return SIN_TABLE[(int) (f * 10430.38F + 16384F) & 0xffff];
	    }

        public static float Sqrt(float f)
        {
            return (float)Math.Sqrt(f);
        }

        static public int AbsModulo(int value, int floor)
        {
            if (value > 0)
            {
                return value % floor;
            }
            else if (value < 0)
            {
                if (value % floor == 0)
                {
                    return 0;
                }
                else
                {
                    return floor + (value % floor);
                }
            }
            else
            {
                return 0;
            }
        }

        static public float WrapAngleRadians(float value)
        {
            return value % ((float)Math.PI * 2);
        }

        static public float WrapAngleDegrees(float value)
        {
            return value % 360;
        }

        static public float Clamp(float value, float min, float max)
        {
            return Math.Min(Math.Max(value, min), max);
        }
    }
}
